﻿using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using DrawingObjects.Properties;
using XMLParsers.SAX;

namespace DrawingObjects
{
    public partial class FormDrawingObjects : Form
    {
        private Color _color;

        public FormDrawingObjects()
        {
            InitializeComponent();
        }
        
        private void FormDrawingObjectsLoad(object sender, EventArgs e)
        {
            PictureBoxMain.BackColor = Color.White;
            PictureBoxMain.Refresh();
            _color = Color.Black;
            ComboBoxWidth.SelectedIndex = 0;
        }

        private void ButtonColorClick(object sender, EventArgs e)
        {
            ColorDialogPicker = new ColorDialog { Color = _color };
            ColorDialogPicker.ShowDialog();
            _color = ColorDialogPicker.Color;
            ButtonColor.BackColor = _color;
        }

        private void ToolStripMenuItemClearClick(object sender, EventArgs e)
        {
            PictureBoxMain.Controls.Clear();
            PictureBoxMain.BackColor = Color.White;
            PictureBoxMain.Refresh();
        }

        private void PictureBoxMainMouseDown(object sender, MouseEventArgs e)
        {
            var name = DrawingToolsBoxObjects.GetSelectedObject();
            if (name != null && !name.Equals(""))
            {
                PictureBoxMain.Controls.Add(new DrawingObject(e, name, _color, GetPenWidth()));
            }
        }

        private int GetPenWidth()
        {
            switch (ComboBoxWidth.SelectedIndex)
            {
                case 0: return 1;
                case 1: return 2;
                case 2: return 5;
                case 3: return 10;
            }
            return 1;
        }

        private void ToolStripMenuItemAboutClick(object sender, EventArgs e)
        {
            MessageBox.Show(Resources.AboutDrawingObjects, @"About DrawingObjects", 
                MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void ToolStripMenuItemExitClick(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void ToolStripMenuItemSaveObjectsStateAsClick(object sender, EventArgs e)
        {
            SaveFileDialogDrawingObjects.Filter = Resources.FilterForImages;
            SaveFileDialogDrawingObjects.Title = @"Saving objects state...";
            if (SaveFileDialogDrawingObjects.ShowDialog() != DialogResult.OK) return;
            var fileStream = new FileStream(SaveFileDialogDrawingObjects.FileName, FileMode.Create);
            var streamWriter = new StreamWriter(fileStream);
            streamWriter.WriteLine("<XML>");
            foreach (var item in PictureBoxMain.Controls)
                streamWriter.WriteLine(item);
            streamWriter.WriteLine("</XML>");
            streamWriter.Close();
            fileStream.Close();
        }

        private void ToolStripMenuItemOpenObjectsStateClick(object sender, EventArgs e)
        {
            OpenFileDialogDrawingObjects.Filter = Resources.FilterForImages;
            OpenFileDialogDrawingObjects.Title = @"Opening Objects state...";
            PictureBoxMain.Controls.Clear();
            if (OpenFileDialogDrawingObjects.ShowDialog() != DialogResult.Cancel)
            {
                var file = new FileStream(OpenFileDialogDrawingObjects.FileName, FileMode.Open);
                var xml = new XMLParserSimpleReader().ReadXML(file);
                foreach (XMLParserSimpleElement item in xml.ChildXMLElements)
                    PictureBoxMain.Controls.Add(new DrawingObject(((XMLParserSimpleElement)item.ChildXMLElements[0]).ElementString,
                        int.Parse(((XMLParserSimpleElement)item.ChildXMLElements[1]).ElementString),
                        int.Parse(((XMLParserSimpleElement)item.ChildXMLElements[2]).ElementString),
                        int.Parse(((XMLParserSimpleElement)item.ChildXMLElements[3]).ElementString),
                        int.Parse(((XMLParserSimpleElement)item.ChildXMLElements[4]).ElementString),
                        int.Parse(((XMLParserSimpleElement)item.ChildXMLElements[5]).ElementString),
                        int.Parse(((XMLParserSimpleElement)item.ChildXMLElements[6]).ElementString)));
                file.Close();
            }
            PictureBoxMain.Refresh();
        }
    }
}
